Skip to main content

Conditional Expressions

caution

This feature is available starting from QQL 5.6.

Basic

Similarly to ternary operators in Python, you can use if ... else conditional expressions to test a simple condition:

note

Condition under if must be a boolean expression.

select 'few' if n < 3 else 'many', n
array join [0, 1, 2, 3, 4] as n

Switch Case

Similarly to SQL, you can use QQL conditional expressions to test several cases:

Synopsis
CASE expression
WHEN value THEN result
[WHEN...]
[ELSE result]
END
note
  • Types of THEN and ELSE expressions must be equal (for example, all float or all arrays of int32).
  • If several WHEN expressions are true, the first corresponding THEN result is selected.
select case n
when 0 then 'zero'
when 1 then 'one'
when 2 then 'two'
else 'many'
end, n
array join [0, 1, 2, 3, 4] as n

If case expression is empty, expressions under WHEN must return boolean values:

select case
when n == 0 then 'zero'
when n == 1 then 'one'
when n == 2 then 'two'
else 'many'
end, n
array join [0, 1, 2, 3, 4] as n
# another format of writing the expression in the first section using Switch Case keywords:
select case when n < 3 then 'few' else 'many' end, n
array join [0, 1, 2, 3, 4] as n
# other examples
with
(case size(entries)
when 1 then 'one'
when 2 then 'two'
when 3 then 'three'
else 'many'
end) as t
select t, count{}()
from KRAKEN
group by t

select case
when size(entries[this is L1Entry]) > 0 then 'L1'
when size(entries[this is L2EntryNew or this is L2EntryUpdate]) > 0 then 'L2'
end, entries
from KRAKEN